package org.jeecg.system.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.util.ChangeLog;
import org.jeecg.system.common.service.IChangeLogsService;
import org.jeecg.system.project.entity.ProjectSummary;
import org.jeecg.system.project.entity.ProjectTeam;
import org.jeecg.system.project.mapper.ProjectSummaryMapper;
import org.jeecg.system.project.service.IProjectSummaryService;
import org.jeecg.system.project.service.IProjectTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @Description: 招商项目总表
 * @Author: jeecg-boot
 * @Date:   2020-06-11
 * @Version: V1.0
 */

@Service
public class ProjectSummaryServiceImpl extends ServiceImpl<ProjectSummaryMapper, ProjectSummary> implements IProjectSummaryService {

   @Autowired
   private IProjectTeamService projectTeamservice;
   @Autowired
   private IChangeLogsService changeLogService;


    /**
     * 保存招商项目
     * @param project
     * @return
     */
    @Transactional //开启事务，项目报错不保存
    @Override
    public boolean saveProject(ProjectSummary project) {
        project.setFlags(1);
        project.setProjectYear(DateUtils.getYear()+"");//创建的时候默认当前年份
        project.setAuditStatue(0);
        project.setIsLd(0+"");
        if(StringUtils.isBlank(project.getProjectStage())){
            project.setProjectStage("S01");
        }

        boolean save = this.save(project);
        if(save){
            //更新分队负责项目表
            List<ProjectTeam> teamList = project.getTeamList();

            String id = project.getId();
            boolean b = projectTeamservice.saveOrUpdateTeam(teamList, id,project.getTotalAmount());
            if (!b) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }


        }

        return true;
    }

    /**
     * 招商项目的修改
     * @param project
     * @return
     */
    @Transactional
    @Override
    public boolean updateProject(ProjectSummary project) {
        String id = project.getId();
        if(StringUtils.isBlank(id)){

            return  false;
        }

        /**
         * 获取旧项目的值
         */
        ProjectSummary oldProject = this.getById(id);
        //保存修改
        boolean code = this.updateById(project);
        boolean logscode=false;
        if (code) {
            //保存到修改日志表
            logscode = changeLogService.saveChangeLogs(ProjectSummary.class, oldProject, project, "招商项目", project.getId());
        }

        //判断项目阶段是否改变，改变需要重新审核，审核状态改为0
        if (!oldProject.getProjectStage().trim().equals(project.getProjectStage().trim())) {
            project.setAuditStatue(0);//改为未审核状态
        }

        return logscode;
    }

    /**
     * 删除项目，伪删
     * @param id
     * @return
     */
    @Transactional
    @Override
    public boolean deleteProject(String id) {

        UpdateWrapper<ProjectSummary> updateWrapper =new UpdateWrapper<ProjectSummary>();
        updateWrapper.eq("id",id);
        updateWrapper.eq("flags",0);
        boolean code = this.update(updateWrapper);
        boolean updatecode=false;
        if (code) {
            //同步更新 分队负责项目表
            UpdateWrapper<ProjectTeam> projectteam = new UpdateWrapper<ProjectTeam>();
            projectteam.eq("project_id",id);
            projectteam.eq("flags",0);
            updatecode = projectTeamservice.update(projectteam);
            if (!updatecode) {
                //手动回滚是事务
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }


        return updatecode;
    }




    /**
     * 自定义联表查询
     * @param page
     * @param queryWrapper
     * @return
     */
    public IPage<ProjectSummary> pageQueryBySQL(IPage<ProjectSummary> page, Wrapper<ProjectSummary> queryWrapper){
        return page.setRecords(baseMapper.selectByTeam(page,queryWrapper));
    }
}
